package com.eeds.service.proclamation.impl;


import com.eeds.common.model.PageInfo;
import com.eeds.common.table.ProclamationEntity;
import com.eeds.common.table.SysFilesEntity;
import com.eeds.dao.ProclamationDao;
import com.eeds.dao.SysFilesDao;
import com.eeds.date.DateUtil;
import com.eeds.model.proclamation.req.*;
import com.eeds.model.proclamation.res.ResGetProclamationListVO;
import com.eeds.model.proclamation.res.ResGetProclamationVO;
import com.eeds.model.user.res.ResGetUserInfoListVO;
import com.eeds.service.proclamation.ProclamationService;
import com.eeds.service.upload.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;

@Service
public class ProclamationServiceImpl implements ProclamationService {


    @Autowired
    private ProclamationDao proclamationDao;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private SysFilesDao sysFilesDao;

    /**
     * 添加公告
     *
     * @param reqAddProclamationVO
     * @return
     */
    @Override
    @Transactional
    public Integer addProclamation(ReqAddProclamationVO reqAddProclamationVO) {

        ProclamationEntity proclamationEntity = new ProclamationEntity();
        proclamationEntity.setTitle(reqAddProclamationVO.getTitle());
        proclamationEntity.setType(reqAddProclamationVO.getType());
        Date date = DateUtil.nowDate();
        proclamationEntity.setCreateTime(date);
        proclamationEntity.setUpdateTime(date);
        proclamationEntity.setIdxFiles(reqAddProclamationVO.getIdxFiles());
        proclamationEntity.setIdxUserBaseInfo(reqAddProclamationVO.getUserId());
        proclamationEntity.setProclamationType(reqAddProclamationVO.getProclamationType());
        proclamationDao.insert(proclamationEntity);
        

        uploadService.updateFileStatus(reqAddProclamationVO.getIdxFiles());
        return 1;
    }


    /**
     * 查询公告列表
     *
     * @param reqGetProclamationListVO
     * @return
     */
    @Override
    public PageInfo<ResGetProclamationListVO> getProclamationList(ReqGetProclamationListVO reqGetProclamationListVO) {

        PageInfo<ResGetProclamationListVO> pageInfo = new PageInfo<>(reqGetProclamationListVO);
        pageInfo.setData(proclamationDao.getProclamationList(pageInfo,reqGetProclamationListVO));
        pageInfo.setTotalResult(proclamationDao.getProclamationListTotalCount(reqGetProclamationListVO));
        return pageInfo;
    }

    /**
     * 获取公告信息
     *
     * @param reqGetProclamationVO
     * @return
     */
    @Override
    public ResGetProclamationVO getProclamation(ReqGetProclamationVO reqGetProclamationVO) {
        return proclamationDao.getProclamation(reqGetProclamationVO);
    }

    /**
     * 修改公告信息
     *
     * @param reqUpdateProclamationVO
     * @return
     */
    @Override
    @Transactional
    public Integer updateProclamation(ReqUpdateProclamationVO reqUpdateProclamationVO) {
        ProclamationEntity proclamationEntity = new ProclamationEntity();
        proclamationEntity.setTitle(reqUpdateProclamationVO.getTitle());
        proclamationEntity.setType(reqUpdateProclamationVO.getType());
        Date date = DateUtil.nowDate();
        proclamationEntity.setUpdateTime(date);
        proclamationEntity.setIdxFiles(reqUpdateProclamationVO.getIdxFiles());
        proclamationEntity.setIdxUserBaseInfo(reqUpdateProclamationVO.getUserId());
        proclamationEntity.setProclamationType(reqUpdateProclamationVO.getProclamationType());
        proclamationEntity.setIdProclamation(reqUpdateProclamationVO.getIdProclamation());
        proclamationDao.updateById(proclamationEntity);
        uploadService.updateFileStatus(reqUpdateProclamationVO.getIdxFiles());
        return 1;
    }

    /**
     * 删除公告信息
     *
     * @param reqDelProclamationVO
     * @return
     */
    @Override
    @Transactional
    public Integer delProclamation(ReqDelProclamationVO reqDelProclamationVO) {
        ProclamationEntity proclamationEntity = proclamationDao.selectById(reqDelProclamationVO.getIdProclamation());
        SysFilesEntity sysFilesEntity = sysFilesDao.selectById(proclamationEntity.getIdxFiles());
        proclamationDao.deleteById(reqDelProclamationVO.getIdProclamation());
        File file  = new File(sysFilesEntity.getFilePath());
        file.delete();
        return 1;
    }
}
